home *** CD-ROM | disk | FTP | other *** search
- /* linfit(XYpoints)
- Fit data to a straight line using linear regression formula.
- */
-
- #include "callback.h"
- #include <SANE.h> // needed for sqrt()
-
- BOOL linfit(extended *retval,funptr callback)
- {
- EXPR arr;
- extended x,y,*iptr,*jptr,intercept;
- extended sumx,sumxx,sumy,sumyy,sumxy,Sxx,Sxy;
- BOOL isarray;
- long n,count;
-
- sumx=0; sumxx=0; sumy=0; sumyy=0; sumxy=0;
- n = 0;
- MakeParmExpr(0,&arr,callback);
- ProbeExpr(arr,&x,&isarray,&count,callback);
- if(!isarray || count<2)
- {
- ErrMsg(" linfit() expects array of {x,y}",0L,callback);
- FreeExpr(arr,callback);
- return(FALSE);
- }
- AddIndex(&arr,&iptr,callback); // arr[i] is {x,y}
- AddIndex(&arr,&jptr,callback); // j picks x or y
- while(count--)
- {
- *jptr = 1;
- if(EvalExpr(arr,&x,callback) && x==x)
- {
- *jptr = 2;
- if(EvalExpr(arr,&y,callback) && y==y)
- {
- sumx += x;
- sumxx += x*x;
- sumy += y;
- sumyy += y*y;
- sumxy += x*y;
- n++;
- }
- }
- *iptr += 1;
- }
- Sxx=n*sumxx-sumx*sumx;
- Sxy=n*sumxy-sumx*sumy;
-
- SetVarVal("slope",Sxy/Sxx,callback);
- intercept=(sumxx*sumy-sumx*sumxy)/(n*sumxx-sumx*sumx);
- SetVarVal("intercept",intercept,callback);
- *retval = Sxy/sqrt(Sxx*(n*sumyy-sumy*sumy)); // return correlation coeff
-
- FreeExpr(arr,callback);
- return(TRUE);
- }
-
- void main(funptr callback)
- {
- AddXfun("linfit","XYpoints",&linfit,NULL,callback);
- }
-
-